package com.anyou.mall.goo.service.impl;

import java.util.List;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.mall.goo.domain.GooGoodsSuitPlan;
import com.anyou.mall.goo.goods.domain.GooGoods;
import com.anyou.mall.goo.goods.mapper.GooGoodsMapper;
import com.anyou.mall.goo.goods.mapper.LiGoodsMapper;
import com.anyou.mall.goo.goodsSku.domain.GooGoodsSku;
import com.anyou.mall.goo.goodsSku.mapper.GooGoodsSkuMapper;
import com.anyou.mall.goo.mapper.GooGoodsSuitPlanMapper;
import com.anyou.mall.goo.param.AddGoodsSuitParam;
import com.anyou.mall.goo.vo.GoodsSuitDetailsVo;
import com.anyou.mall.goo.vo.GoodsSuitListVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.anyou.mall.goo.mapper.GoodsSuitMapper;
import com.anyou.mall.goo.domain.GoodsSuit;
import com.anyou.mall.goo.service.IGoodsSuitService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 套装商品Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-04-02
 */
@Service
public class GoodsSuitServiceImpl implements IGoodsSuitService
{
    @Autowired
    private GoodsSuitMapper goodsSuitMapper;

    @Autowired
    private GooGoodsSuitPlanMapper gooGoodsSuitPlanMapper;

    @Autowired
    private LiGoodsMapper gooGoodsMapper;

    @Autowired
    private GooGoodsSkuMapper gooGoodsSkuMapper;

    /**
     * 查询套装商品
     *
     * @param goodsSuitId 套装商品主键
     * @return 套装商品
     */
    @Override
    public GoodsSuitDetailsVo selectGoodsSuitByGoodsSuitId(Long goodsSuitId)
    {
        GoodsSuitDetailsVo goodsSuitDetailsVo = new GoodsSuitDetailsVo();
        GoodsSuit goodsSuit = goodsSuitMapper.selectGoodsSuitByGoodsSuitId(goodsSuitId);
        if (goodsSuit != null) {
            BeanUtils.copyProperties(goodsSuit,goodsSuitDetailsVo);
            List<GoodsSuitListVo> goodsSuitListVos = goodsSuitMapper.selectGoodsSuitListByGoodsSuitId(goodsSuitId);
            goodsSuitDetailsVo.setGoodsList(goodsSuitListVos);

        }
        return goodsSuitDetailsVo;
    }

    /**
     * 查询套装商品列表
     *
     * @param goodsSuit 套装商品
     * @return 套装商品
     */
    @Override
    public List<GoodsSuit> selectGoodsSuitList(GoodsSuit goodsSuit)
    {
        return goodsSuitMapper.selectGoodsSuitList(goodsSuit);
    }

    /**
     * 新增套装商品
     *
     * @param goodsSuit 套装商品
     * @return 结果
     */
    @Override
    public int insertGoodsSuit(GoodsSuit goodsSuit)
    {
        goodsSuit.setCreateTime(DateUtils.getNowDate());
        return goodsSuitMapper.insertGoodsSuit(goodsSuit);
    }

    /**
     * 修改套装商品
     *
     * @param goodsSuit 套装商品
     * @return 结果
     */
    @Override
    public int updateGoodsSuit(GoodsSuit goodsSuit)
    {
        goodsSuit.setUpdateTime(DateUtils.getNowDate());
        return goodsSuitMapper.updateGoodsSuit(goodsSuit);
    }

    /**
     * 批量删除套装商品
     *
     * @param goodsSuitIds 需要删除的套装商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSuitByGoodsSuitIds(Long[] goodsSuitIds)
    {
        return goodsSuitMapper.deleteGoodsSuitByGoodsSuitIds(goodsSuitIds);
    }

    /**
     * 删除套装商品信息
     *
     * @param goodsSuitId 套装商品主键
     * @return 结果
     */
    @Override
    public int deleteGoodsSuitByGoodsSuitId(Long goodsSuitId)
    {
        return goodsSuitMapper.deleteGoodsSuitByGoodsSuitId(goodsSuitId);
    }

    @Override
    public ApiResult<List<GooGoods>> goodsName(Long storeId) {
        List<GooGoods> gooGoods = gooGoodsMapper.selectList(new LambdaQueryWrapper<GooGoods>()
                .eq(GooGoods::getStoreId, storeId)
                .eq(GooGoods::getAuditStatus, 1));
        return ApiResult.success(gooGoods);
    }

    @Override
    public ApiResult goodsSkuList(Long goodsId) {
        List<GooGoodsSku> gooGoodsSku = gooGoodsSkuMapper.selectList(new LambdaQueryWrapper<GooGoodsSku>().eq(GooGoodsSku::getGoodsId, goodsId));
        return ApiResult.success(gooGoodsSku);
    }

    @Override
    @Transactional
    public ApiResult addGoodsSuit(AddGoodsSuitParam suitParam) {
        GoodsSuit goodsSuit = new GoodsSuit();
        goodsSuit.setSuitName(suitParam.getSuitName());
        goodsSuit.setStoreId(suitParam.getStoreId());
        goodsSuit.setCarPartsShopId(suitParam.getCarPartsShopId());
        goodsSuit.setGoodsCount(suitParam.getGoodsCount());
        goodsSuit.setSuitPrice(suitParam.getSuitPrice());
        goodsSuit.setShelfStatus(suitParam.getShelfStatus());
        int i=suitParam.getSuitPlan().size()- 1;
        goodsSuit.setGoodsCount( new Long((long)i+1));
         goodsSuitMapper.insert(goodsSuit);
        suitParam.getSuitPlan().forEach(e->{
            GooGoodsSuitPlan GoodsSuitPlan = new GooGoodsSuitPlan();
            BeanUtils.copyProperties(e,GoodsSuitPlan);
            GoodsSuitPlan.setGoodsSuitId(goodsSuit.getGoodsSuitId());
            gooGoodsSuitPlanMapper.insert(GoodsSuitPlan);
        });

        return ApiResult.success("新增成功");
    }

    @Override
    @Transactional
    public ApiResult updateGoodsSuit(AddGoodsSuitParam suitParam) {
        goodsSuitMapper.update(null,new LambdaUpdateWrapper<GoodsSuit>()
                .set(GoodsSuit::getSuitName,suitParam.getSuitName())
                .set(GoodsSuit::getGoodsCount,suitParam.getSuitPlan().size())
                .set(GoodsSuit::getSuitPrice,suitParam.getSuitPrice())
                .set(GoodsSuit::getShelfStatus,suitParam.getShelfStatus())
                .eq(GoodsSuit::getGoodsSuitId,suitParam.getGoodsSuitId()));
        suitParam.getSuitPlan().forEach(e->{
            /*GooGoodsSuitPlan update = new GooGoodsSuitPlan();
            BeanUtils.copyProperties(e,update);
            gooGoodsSuitPlanMapper.deleteById(update);*/

        });
        gooGoodsSuitPlanMapper.delete(new LambdaUpdateWrapper<GooGoodsSuitPlan>()
                .eq(GooGoodsSuitPlan::getGoodsSuitId,suitParam.getGoodsSuitId()));
        suitParam.getSuitPlan().forEach(e->{
            GooGoodsSuitPlan add = new GooGoodsSuitPlan();
            BeanUtils.copyProperties(e,add);
            add.setGoodsSuitId(suitParam.getGoodsSuitId());
            gooGoodsSuitPlanMapper.insert(add);
        });
        return ApiResult.success("修改成功");
    }

    @Override
    public ApiResult shelfStatus(GoodsSuit goodsSuit) {
        return ApiResult.success(goodsSuitMapper.update(null,new LambdaUpdateWrapper<GoodsSuit>()
                .set(GoodsSuit::getShelfStatus,goodsSuit.getShelfStatus())
                .eq(GoodsSuit::getGoodsSuitId,goodsSuit.getGoodsSuitId())));
    }

    @Override
    public ApiResult updateEnableStatus(Long goodsSuitId) {

        GoodsSuit goodsSuit = goodsSuitMapper.selectGoodsSuitByGoodsSuitId(goodsSuitId);
        if (goodsSuit != null) {
            if ("0".equals(goodsSuit.getShelfStatus())) {
                goodsSuit.setShelfStatus("1");
            } else {
                goodsSuit.setShelfStatus("0");
            }
            return ApiResult.success(goodsSuitMapper.updateGoodsSuit(goodsSuit));
        } else {
            return ApiResult.error("修改失败");
        }
    }
}
